package cn.fl.custpay.service.impl;

import java.util.*;

import cmm.comm.facade.livecheck.api.ICmmLiveChkApiService;
import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.custpay.service.ICmmLiveChkService;
import cn.fl.custpay.transform.CmmLiveChkVOTransform;
import cn.fl.custpay.vo.CmmLiveChkVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.service.ISysDictionaryService;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.vo.SysUlFilesRecVO;
import cn.fl.system.vo.SysUsrVO;
import fl.constant.facade.framework.constant.LiveCheckConstant;
import fl.constant.facade.framework.constant.UploadConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.livecheck.dto.CmmLiveChkDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;


/**
 * @Description: 活体检测表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class CmmLiveChkServiceImpl implements ICmmLiveChkService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(CmmLiveChkServiceImpl.class);
	
	/**
	 *  活体校验API服务
	 */
	@Autowired
	private ICmmLiveChkApiService cmmLiveChkApiService;
	@Autowired
	private ISysUlFilesRecService sysUlFilesRecService;

	/**
	 *  数据字典服务
	 */
	@Autowired
	private ISysDictionaryService sysDictionaryService;

	@Autowired
	private ICstBscInfoService cstBscInfoService;
	@Autowired
	private ISysUsrService sysUsrService;

	@Override
	public CmmLiveChkVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
		}
		//TODO 业务逻辑
		CmmLiveChkDTO dto = null;//this.自定义ApiService.selectCmmLiveChkDTOById(id);
		return CmmLiveChkVOTransform.toVO(dto);
	}

	@Override
	public Long add(CmmLiveChkVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不存在!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		};
		//return this.cmmLiveChkApiService.add(CmmLiveChkVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除对象主键不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		//return this.cmmLiveChkApiService.removeCmmLiveChkDTOById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		};
		return false;
    }

	@Override
	public boolean modify(CmmLiveChkVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		};
		//return this.cmmLiveChkApiService.modifyCmmLiveChkDTO(CmmLiveChkVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		Pager pager = this.cmmLiveChkApiService.searchCmmLiveChkListPage(page);
		List<CmmLiveChkDTO> list = (List<CmmLiveChkDTO>) pager.getResultList();
		/***
		 *	数据字典CD名称转换
		 **/
		//数据字典（系统名+是否启用）
		List<String> codeGrpCdList = new ArrayList<>();
		//终端平台
		codeGrpCdList.add(DictionaryConstant.CODE_OP_TYP_CD);
		//数据来源
		codeGrpCdList.add(DictionaryConstant.CODE_SYS_DS_CD);
		//活体评分级别
		codeGrpCdList.add(PropertiesCacheUtils.getProperty(LiveCheckConstant.LIVE_LEVEL_CD));
		//活体业务类型
		codeGrpCdList.add(PropertiesCacheUtils.getProperty(LiveCheckConstant.BUS_TYPE_CD));
		//合同签署渠道
		codeGrpCdList.add(PropertiesCacheUtils.getProperty(LiveCheckConstant.CONTRACT_CHANNEL_CD));
		//活体审核状态
		codeGrpCdList.add(PropertiesCacheUtils.getProperty(LiveCheckConstant.AUDIT_STATUS_CD));

		Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
		List<KeyValueVO> sysList = dicMap.get(DictionaryConstant.CODE_OP_TYP_CD);
		List<KeyValueVO> sourceList = dicMap.get(DictionaryConstant.CODE_SYS_DS_CD);
		List<KeyValueVO> livLevelList = dicMap.get(PropertiesCacheUtils.getProperty(LiveCheckConstant.LIVE_LEVEL_CD));
		List<KeyValueVO> busTypList = dicMap.get(PropertiesCacheUtils.getProperty(LiveCheckConstant.BUS_TYPE_CD));
		List<KeyValueVO> channelList = dicMap.get(PropertiesCacheUtils.getProperty(LiveCheckConstant.CONTRACT_CHANNEL_CD));
		List<KeyValueVO> auditList = dicMap.get(PropertiesCacheUtils.getProperty(LiveCheckConstant.AUDIT_STATUS_CD));
		Map<String, String> sysMap = null;
		if (CollectionUtils.isNotEmpty(sysList)) {
			sysMap = new HashMap<>(sysList.size());
			for (KeyValueVO dto : sysList) {
				sysMap.put(dto.getKey(), dto.getValue());
			}
		}
		Map<String, String> sourceMap = null;
		if (CollectionUtils.isNotEmpty(sourceList)) {
			sourceMap = new HashMap<>(sourceList.size());
			for (KeyValueVO dto : sourceList) {
				sourceMap.put(dto.getKey(), dto.getValue());
			}
		}
		Map<String, String> livLevelMap = null;
		if (CollectionUtils.isNotEmpty(livLevelList)) {
			livLevelMap = new HashMap<>(livLevelList.size());
			for (KeyValueVO dto : livLevelList) {
				livLevelMap.put(dto.getKey(), dto.getValue());
			}
		}
		Map<String, String> busTypMap = null;
		if (CollectionUtils.isNotEmpty(busTypList)) {
			busTypMap = new HashMap<>(busTypList.size());
			for (KeyValueVO dto : busTypList) {
				busTypMap.put(dto.getKey(), dto.getValue());
			}
		}
		Map<String, String> channelMap = null;
		if (CollectionUtils.isNotEmpty(channelList)) {
			channelMap = new HashMap<>(channelList.size());
			for (KeyValueVO dto : channelList) {
				channelMap.put(dto.getKey(), dto.getValue());
			}
		}
		Map<String, String> auditMap = null;
		if (CollectionUtils.isNotEmpty(auditList)) {
			auditMap = new HashMap<>(auditList.size());
			for (KeyValueVO dto : auditList) {
				auditMap.put(dto.getKey(), dto.getValue());
			}
		}
		List<String> cstIdList = new ArrayList<>();
		List<String> cmmLivChkIdList = new ArrayList<>();
		List<CmmLiveChkVO> cmmLiveChkVOList = CmmLiveChkVOTransform.toVOList(list);
		for (CmmLiveChkVO cmmLiveChkVO : cmmLiveChkVOList) {
			if (sysMap != null && sysMap.containsKey(cmmLiveChkVO.getContractPlatformCd())) {
				cmmLiveChkVO.setContractPlatformNm(sysMap.get(cmmLiveChkVO.getContractPlatformCd()));
			}
			if (sourceMap != null && sourceMap.containsKey(cmmLiveChkVO.getContractSourceCd())) {
				cmmLiveChkVO.setContractSourceNm(sourceMap.get(cmmLiveChkVO.getContractSourceCd()));
			}
			if (livLevelMap != null && livLevelMap.containsKey(cmmLiveChkVO.getLiveLevelCd())) {
				cmmLiveChkVO.setLiveLevelNm(livLevelMap.get(cmmLiveChkVO.getLiveLevelCd()));
			}
			if (busTypMap != null && busTypMap.containsKey(cmmLiveChkVO.getBusTypeCd())) {
				cmmLiveChkVO.setBusTypeNm(busTypMap.get(cmmLiveChkVO.getBusTypeCd()));
			}
			if (channelMap != null && channelMap.containsKey(cmmLiveChkVO.getContractChannelCd())) {
				cmmLiveChkVO.setContractChannelNm(channelMap.get(cmmLiveChkVO.getContractChannelCd()));
			}
			if (auditMap != null && auditMap.containsKey(cmmLiveChkVO.getAuditStatusCd())) {
				cmmLiveChkVO.setAuditStatusNm(auditMap.get(cmmLiveChkVO.getAuditStatusCd()));
			}

			if(StringUtils.isEmpty(cmmLiveChkVO.getCertNo())){
				cmmLiveChkVO.setCertNo(cmmLiveChkVO.getuCertNo());
			}
			if(StringUtils.isEmpty(cmmLiveChkVO.getCstNm())){
				cmmLiveChkVO.setCstNm(cmmLiveChkVO.getuRlNm());
			}
			if(StringUtils.isEmpty(cmmLiveChkVO.getMp())) {
				//获取客户手机号
				String cstId = cmmLiveChkVO.getCstId();
				Long usrId = null;
				if (StringUtils.isNotEmpty(cstId)) {
					CstBscInfoVO cstBscInfoVO = this.cstBscInfoService.selectById(Long.valueOf(cstId));
					if (null != cstBscInfoVO) {
						usrId = cstBscInfoVO.getUsrId();
					}
				} else {
					usrId = Long.valueOf(cmmLiveChkVO.getUsrId());
				}
				if (StringUtils.isNotEmpty(usrId)) {
					try {
						SysUsrVO sysUsrVO = this.sysUsrService.selectById(usrId, CurrentThreadContext.getCurrentSysCd());
						if (sysUsrVO != null) {
							cmmLiveChkVO.setMp(sysUsrVO.getMp());
						}
					} catch (Exception e) {
						BaseLogUtils.info(logger, "modify", "获取用户信息异常!" + e.getMessage(), e);
					}
				}
			}

			cstIdList.add(cmmLiveChkVO.getCstId());
			cmmLivChkIdList.add(String.valueOf(cmmLiveChkVO.getId()));
		}
		/***
		 *	文件路径获取
		 **/
		//获取身份证照片集合
		Map<String, List<SysUlFilesRecVO>> identifileMap = sysUlFilesRecService.getMultiList(CurrentThreadContext.getFlServiceSysCd(),PropertiesCacheUtils.getProperty(UploadConstant.CFG_LIV_CHK_ORG_NEW_CD),
				PropertiesCacheUtils.getProperty(UploadConstant.CFG_CST_ID_CD),cstIdList);
		//获取活体校验照片集合
		Map<String, List<SysUlFilesRecVO>> livefileMap = sysUlFilesRecService.getMultiList(CurrentThreadContext.getFlServiceSysCd(),PropertiesCacheUtils.getProperty(UploadConstant.CFG_LIV_CHK_ORG_NEW_CD),
				PropertiesCacheUtils.getProperty(UploadConstant.CFG_LIV_CHK_NEW_CD),cmmLivChkIdList);
		for (CmmLiveChkVO cmmLiveChkVO : cmmLiveChkVOList) {
			List<SysUlFilesRecVO> identiFileList = identifileMap.get(cmmLiveChkVO.getCstId());
			cmmLiveChkVO.setIdentiFileList(identiFileList);
			List<SysUlFilesRecVO> livefileList = livefileMap.get(String.valueOf(cmmLiveChkVO.getId()));
			cmmLiveChkVO.setLivefileList(livefileList);
		}
		pager.setResultList(cmmLiveChkVOList);
		return pager;
	}
}
